package org.lanqiao.taru.service;

import lombok.extern.slf4j.Slf4j;
import org.lanqiao.taru.annotation.Monitor;
import org.lanqiao.taru.dao.UserinfoMapper;
import org.lanqiao.taru.domain.TbUserinfo;
import org.lanqiao.taru.vo.AuthUserVo;
import org.lanqiao.taru.vo.UserinfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


/**
 * 把这个类放到ioc容器中去，   HashMap  map =new HashMap();   map.put("UserinfoService",new UserinfoService())
 */
@Slf4j
@Service

public   class UserinfoService {

    @Autowired
    UserinfoMapper  userinfoMapper;

    /**
     * 查询所有的数据
     * @return
     */

    public List<TbUserinfo>   getUserinfosList(){
        Example  example  =new Example(TbUserinfo.class);
        example.orderBy("createDate").desc();
        List<TbUserinfo>     list    =   userinfoMapper.selectByExample(example);

        return  list;
    }


    /**
     * 查询分页数据
     * @return
     */
    public List<TbUserinfo>   getUserinfosPage(){
        List<TbUserinfo>     list=  userinfoMapper.selectAll();
        return  list;
    }


    /**
     * 根据主键查询
     * @param id
     * @return
     */
    @Monitor
    public TbUserinfo   getUserinfoById(Long  id){
        TbUserinfo    userinfo =  userinfoMapper.selectByPrimaryKey(id);
        return  userinfo;
    }


    /**
     * 增加
     * @param tbUserinfo
     */
    public  void     saveUserinfo(TbUserinfo  tbUserinfo){
        userinfoMapper.insert(tbUserinfo);
        //  userinfoMapper.insertSelective(tbUserinfo);

    }


    /**
     * 修改
     * @param tbUserinfo
     */
    @Transactional
    public void     updateUserinfo(TbUserinfo  tbUserinfo ){
        userinfoMapper.updateByPrimaryKey(tbUserinfo);

    }


    /**
     * 删除
     * @param id
     */
    public void    removeUserinfo(Long  id){
        userinfoMapper.deleteByPrimaryKey(id);
    }


    /**
     * 登录
     * @param authUserVo
     * @return
     */
    public TbUserinfo  login(AuthUserVo  authUserVo){

        Example  example  =new Example(TbUserinfo.class);
        Example.Criteria  criteria  = example.createCriteria();
        criteria.andEqualTo("username",authUserVo.getUsername());
        criteria.andEqualTo("password",authUserVo.getPassword());
        TbUserinfo  tbUserinfo  = userinfoMapper.selectOneByExample(example);
        return   tbUserinfo;

    }


    /**
     * 使用SQL语句形式
     * @param authUserVo
     * @return
     */
    public TbUserinfo  login2(AuthUserVo  authUserVo){
        return   userinfoMapper.selectByUsernameAndPassword(authUserVo);

    }


    public TbUserinfo  login3(String username  ,String password){
        return   userinfoMapper.selectByUsernameAndPassword2(username,password);

    }


    public   TbUserinfo  selectUserinfoWithArticlesById(Long  id){
        return   userinfoMapper.selectUserinfoWithArticlesById(id);
    }


    public   List<TbUserinfo> getUserinfosByCondition(UserinfoVo vo){
        return   userinfoMapper.getUserinfosByCondition(vo);

    }


    /**
     * 根据编号数组批量删除用户信息
     * @param idList
     */

    public   void   removeBatchById(List<Long>   idList){
            userinfoMapper.deleteBatchById(idList);
    }


    /**
     * 转账
     * @param id 进id
     * @param id2 出id
     * @param money
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor= Exception.class,readOnly = false  )
    public  void    transfer(Integer  id ,Integer  id2 ,Integer  money){
        try{
            userinfoMapper.add(id,money);
            userinfoMapper.minus(id2,money);
            a();

        }catch (Exception ex){
            throw   ex;

        }

    }


    @Transactional
    public void  a(){

    }



}
